﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dal;
using System.Threading;
using System.Net;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            BankAccount account = new BankAccount();

            Task[] tasks = new Task[10];
            for (int i = 0; i < 10; i++)
            {
                tasks[i] = new Task(()=> {
                    for (int j = 0; j < 1000; j++)
                    {
                        account.Balance++;
                    }
                });
                tasks[i].Start();
            }

            Task.WaitAll(tasks);
            Console.WriteLine("Expected value{0},Counter value:{1}",10000,account.Balance);


            Console.ReadLine();
        }
        static async void Test()
        {
            Console.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
            int a = await Get1();
            int b = await Get2();
            int c = await Get3();
           // Console.WriteLine(a+b+c);
            Console.WriteLine(await Get1() + await Get2() + await Get3());
            Console.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
        }
        static async Task<int> Get1()
        {
          await  Task.Delay(2000);
            return 1;
        }
        static async Task<int> Get2()
        {
            await Task.Delay(2000);
            return 2;
        }
        static async Task<int> Get3()
        {
            await Task.Delay(3000);
            return 3;
        }
        static async Task<int> CountCharsAsync(string url)
        {
            WebClient wc = new WebClient();
            string result = await wc.DownloadStringTaskAsync(new Uri(url));
            return result.Length;
        }
        static async Task<int> GetLengthAsync()
        {
            Console.WriteLine("GetLengthAsync Start");
            string str = await GetStringAsync();
            Console.WriteLine("GetLengthAsync End");
            return str.Length;
        }
        static async Task<string> GetStringAsync()
        {
            return await Task<string>.Run(() => { Task.Delay(2000); return "finished"; });
        }
        static void DoRun1()
        {
            Console.WriteLine("Thread Id =" + Thread.CurrentThread.ManagedThreadId);
        }

        static void DoRun2()
        {
            Thread.Sleep(50);
            Console.WriteLine("Task调用Thread Id =" + Thread.CurrentThread.ManagedThreadId);
        }
    }
    public class BankAccount
    {
        public  int Balance { get; set; }
    }

}
